<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="btn" onclick="clickBtn()">点击我</button>
    <iframe id="iframe" src="http://erp.office.51fanli.com/" frameborder="0" width="300" height="300" style="background: red;">

    </iframe>

    <script>
        function clickBtn (){
            console.log("点击了按钮")
        }

        let btn = document.getElementById('btn')
        btn.click()

        new Promise((reslove)=>{
            console.log("同步任务")
            reslove()
        }).then(()=>{
            console.log("promise 异步微任务")
        })


        

        setTimeout(()=>{
            console.log("setTimout 异步宏任务")
        }, 1000)

        const ch = new MessageChannel()
        const port1 = ch.port1
        const port2 = ch.port2

        port1.onmessage = function(d){
            console.log(`port1接受的消息是: ${d.data}`)
        }

        port2.onmessage = function(d){
            console.log(typeof d.data)
            if(typeof d.data === 'object'){
               console.log(d.data)
               console.log(d.data === obj)
            }else{
                console.log(`port2接受的消息是: ${d.data}`)
            }
           
        }

        const obj = {
            a: 1,
            b: 2
        }
        port1.postMessage(obj)
        port1.postMessage("port1发送的消息")
        port2.postMessage("port2发送的消息")
        port2.postMessage("port2发送的第2条消息")

        // let ifr = document.getElementById('iframe')
        // ifr.addEventListener('load', iframeLoaded, false)
        // function iframeLoaded(){
        //     let iframeWindow = ifr.contentWindow
        //     iframeWindow.postMessage("iframe窗口发来的消息", "*" , [port2])
        // }
        // window.onmessage = function(d){
        //     console.log(`window接受到的消息是: ${ d.data }`)
        // }


        // const w1 = new Worker('./w1.js')
        // const w2 = new Worker('./w2.js')
        // w1.postMessage('w1发送消息' , [port1])
        // w2.postMessage('w2发送消息' , [port2])

        
    </script>
</body>
</html>